package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.User;
import com.example.utils.ExceptionResult;
import org.apache.ibatis.annotations.Update;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.example.service.IArticleService;
import com.example.entity.Article;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dengc
 * @since 2023-05-16
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

        @Resource
        private IArticleService articleService;

        //新增与修改同一个方法
        @PostMapping
        public ExceptionResult save(@RequestBody Article article) {
            return ExceptionResult.success(articleService.saveOrUpdate(article));
        }

        //删除
        @DeleteMapping("/{id}")
        public ExceptionResult delete(@PathVariable Integer id) {
            return ExceptionResult.success(articleService.removeById(id));
        }

        //查询列表
        @GetMapping
        public ExceptionResult findAll() {
            return ExceptionResult.success(articleService.list());
        }

        //根据id查询单个
        @GetMapping("/{id}")
        public ExceptionResult findOne(@PathVariable Integer id) {
            return ExceptionResult.success(articleService.getById(id));
        }

        //批量删除
        @PostMapping("/del/batch")
        public ExceptionResult deleteBatch(@RequestBody List<Integer> ids){
                return ExceptionResult.success(articleService.removeByIds(ids));
        }

        //分页查询
        @GetMapping("/page")
        public ExceptionResult findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(defaultValue = "") String id,
                                        @RequestParam(defaultValue = "") String useraccount) {
                LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.like(!"".equals(useraccount), Article::getUseraccount,useraccount);
                queryWrapper.like(!"".equals(id), Article::getId,id);
                return ExceptionResult.success(articleService.page(new Page<>(pageNum, pageSize),queryWrapper));
        }
        //分页展示页面
        @GetMapping("/pages")
        public ExceptionResult findPage1(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(defaultValue = "") String articletitle) {
                LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.like(!"".equals(articletitle), Article::getArticletitle,articletitle);
                queryWrapper.eq(Article::getArticleisyes,1);
                queryWrapper.orderByDesc(Article::getId);
                return ExceptionResult.success(articleService.page(new Page<>(pageNum, pageSize),queryWrapper));
        }


}
